// pages/modules/peopleAssigned/infoGather/infoGather.js
var util = require('../../../../utils/util.js')
var _this, system
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cutsomTag1: '',
    cutsomTag2: '',
    cutsomTag3: '',
    avatarUrl:'',
    animation: {},
    initTagSelectedArray1:[],
    initCustomTagSelectedArray1: [],

    initTagSelectedArray2: [],
    initCustomTagSelectedArray2: [],

    initTagSelectedArray3: [],
    initCustomTagSelectedArray3: [],
    currentPage: 0
  },
  //返回页统一处理
  birthdayGoPrePage: function (e) {
    _this.genAnimation(0)
    _this.data.currentPage = 0
  },
  wxGoPrePage: function (e) {
    _this.genAnimation(-system.windowWidth)
    _this.data.currentPage = 1
  },
  tag1GoPrePage: function (e) {
    _this.genAnimation(-system.windowWidth * 2)
    _this.data.currentPage = 2
  },
  tag2GoPrePage: function (e) {
    _this.genAnimation(-system.windowWidth * 3)
    _this.data.currentPage = 3
  },
  tag3GoPrePage: function (e) {
    _this.genAnimation(-system.windowWidth * 4)
    _this.data.currentPage = 4
  },
  sexFinishedHandle: function (e) {
    console.log(e.detail)
    app.userSetting.sex = e.detail.sex
    console.log(app.userSetting.sex)
  },
  avatarChangeSuccessHandle: function (e) {
    console.log(e.detail)
    _this.setData({
      avatarUrl: e.detail.imgUrl
    })
  },
  sexGoNext: function(e) {
    //性别选择页面跳转到下一步
    _this.genAnimation(-system.windowWidth)
    _this.data.currentPage = 1
  },
  birthdayFinishedHandle: function (e) {
    console.log(e.detail)
    if (e.detail.action == 0) {
      //返回上一题
    } else {
      app.userSetting.birthday = e.detail.birthday
      console.log(app.userSetting.birthday)
    }
  },
  birthdayGoNext: function(e) {
    //日期选择页面跳转到下一步
    _this.genAnimation(-system.windowWidth*2)
    _this.data.currentPage = 2
  },

  wxFinishedHandle: function (e) {
    console.log('wx:' + e.detail.content)
    app.userSetting.wx = e.detail.content
    _this.genAnimation(-system.windowWidth * 3)
    _this.data.currentPage = 3
  },
  wxSkipHandle: function (e) {
    _this.genAnimation(-system.windowWidth * 3)
    _this.data.currentPage = 3
  },
  uploadFinishedHandle: function (e) {
    if (e.detail.action == 1) {
      console.log('个人照片上传步骤完成')
      //继续下一步
    } else if (e.detail.action == 2) {
      //继续下一步
    } else {
      //返回上一题
    }
  },
  customTagDidClick: function () {
    wx.navigateTo({
      url: '../infoTagCustom/infoTagCustom',
    })
  },
  tagSelectedFinishedHandle1: function (e) {
    app.userSetting.tagSelectedArray1 = e.detail.tagSelectedArray
    app.userSetting.customTagSelectedArray1 = e.detail.customTagSelectedArray
    //标签已经选择完毕，可以继续下一把
    _this.genAnimation(-system.windowWidth * 4)
    _this.data.currentPage = 4
  },
  tagSelectedFinishedHandle2: function (e) {
    app.userSetting.tagSelectedArray2 = e.detail.tagSelectedArray
    app.userSetting.customTagSelectedArray2 = e.detail.customTagSelectedArray
    //标签已经选择完毕，可以继续下一把
    _this.genAnimation(-system.windowWidth * 5)
    _this.data.currentPage = 5
  },
  tagSelectedFinishedHandle3: function (e) {
    app.userSetting.tagSelectedArray3 = e.detail.tagSelectedArray
    app.userSetting.customTagSelectedArray3 = e.detail.customTagSelectedArray
    //标签已经选择完毕，可以继续下一把
    console.log(app.userSetting)
    _this.updateUserInfo()
  },
  addTag: (tag) => {
    console.log(tag)
    if (_this.data.currentPage == 3) {
      _this.setData({
        cutsomTag1: tag
      })
    } else if (_this.data.currentPage == 4 ) {
      _this.setData({
        cutsomTag2: tag
      })
    } else if (_this.data.currentPage == 5) {
      _this.setData({
        cutsomTag3: tag
      })
    }

  },
  goMoreInfo: function () {
    wx.navigateTo({
      url: '../moreInfo/moreInfo',
    })
  },
  animationWithPageIndex: function (index, direction) {
    var width = system.windowWidth
    var animationOffsetx = 0
    var currentOffset = width * index
    if (direction == 'push') {
      animationOffsetx = currentOffset - width
    } else {
      animationOffsetx = currentOffset + width
    }
    _this.genAnimation(animationOffsetx)
    console.log('animtion offset x:' + animationOffsetx)
  },
  genAnimation: function (offsetX) {
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease-in',
      delay: 0
    })
    animation.translateX(offsetX).step()
    _this.setData({
      animation: animation.export()
    })
  },
  updateUserInfo: function() {
    var data = {}
    data.sex = app.userSetting.sex
    data.birthday = app.userSetting.birthday
    data.university = app.userSetting.university
    data.major = app.userSetting.major
    data.wx = app.userSetting.wx
    data.address = app.userSetting.address
    //第一页标签对应labels2
    data.labels2 = app.userSetting.tagSelectedArray1.join()
    if (app.userSetting.customTagSelectedArray1.length > 0) {
      data.otherLabels2 = app.userSetting.customTagSelectedArray1.join()
    }
    //第二页标签对应labels3
    data.labels3 = app.userSetting.tagSelectedArray2.join()
    if (app.userSetting.customTagSelectedArray2.length > 0) {
      data.otherLabels3 = app.userSetting.customTagSelectedArray2.join()
    }
    //第三页标签对应labels
    data.labels = app.userSetting.tagSelectedArray3.join()
    if (app.userSetting.customTagSelectedArray3.length > 0) {
      data.otherLabels = app.userSetting.customTagSelectedArray3.join()
    }
    util.updateUserInfo(_this.requestDidResponse, _this.requestDidFaild, data)
    wx.showLoading({
      title: '资料保存中...',
    })
  },
  requestDidResponse: (res) => {
    wx.hideLoading()
    if (res.status == '200') {
      wx.navigateTo({
        url: '../matchResult/matchResult',
      })
    }else{
      wx.showToast({
        title: '保存失败，请重试',
        icon: 'none' 
      })
    }
  },
  requestDidFaild: (res) => {
    wx.hideLoading()
    wx.showToast({
      title: '网络走神了，请稍候再试哦',
      icon: 'none'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this
    system = wx.getSystemInfoSync()
    _this.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})